<template>
  <div class="photo-con">
    <!-- 顶部的横向滑动区域 -->
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <a
            @tap="getPhotoListByCateId(item.id)"
            :class="[
              'mui-control-item',
              cateId === item.id ? 'mui-active' : '',
            ]"
            v-for="item in cates"
            :key="item.id"
          >
            {{ item.title }}
          </a>
        </div>
      </div>
    </div>
    <!-- 中间的图片列表内容 -->
    <ul class="photo-list">
      <li v-for="item in list" :key="item.id">
        <router-link :to="{ name: 'photo_show', params: { id: item.id } }">
          <!-- 用src  图片会一张一张的出来 有lazy 图片会所有加载完了之后 全部显示出来-->
          <img v-lazy="item.img_url" alt="" />
          <div class="info">
            <h1 class="info-title">
              {{ item.title }}
            </h1>
            <div class="info-body">{{ item.zhaiyao }}</div>
          </div>
        </router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cates: [], //保存所有的分类的数据
      list: [], //保存所有的列表
      cateId: 0, // 保存需要查询的分类id
    };
  },
  created() {
    this.getAllCategory();
    this.getPhotoListByCateId(0); //0代表 传入的id 是全部
  },
  methods: {
    // 发送请求 获取所有的图片分享分类
    getAllCategory() {
      this.$http.get("photo/getimgcategory").then((res) => {
        if (res.data.status === 0) {
          this.cates = res.data.message;
          this.cates.unshift({
            id: 0,
            title: "全部",
          });
          // console.log(this.cates);
        } else {
          this.$toast("加载失败");
        }
      });
    },
    getPhotoListByCateId(cateId) {
      this.cateId = cateId;
      // 根据我们的分类id 发送请求获取图片列表
      this.$http
        .get("photo/getimages", { params: { id: this.cateId } })
        .then((res) => {
          if (res.data.status === 0) {
            this.list = res.data.message;
          } else {
            this.$toast("加载失败");
          }
        });
    },
  },
};
</script>

 <style lang="scss">
* {
  touch-action: pan-y; /*去除警告*/
}
.photo-list {
  list-style: none;
  margin: 0;
  padding: 10px;
  padding-bottom: 0;
  li {
    background-color: #ccc;
    text-align: center;
    margin-bottom: 5px;
    box-shadow: 0 0 9px #999;
    position: relative;
    img {
      width: 100%;
      vertical-align: middle;
    }
    img[lazy="loading"] {
      width: 40px;
      height: 300px;
      margin: auto;
    }
    .info {
      color: white;
      text-align: left;
      position: absolute;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.4);
      max-height: 84px;
      .info-title {
        font-size: 14px;
      }
      .info-body {
        font-size: 13px;
      }
    }
  }
}
</style>

